﻿Imports System
Imports System.ComponentModel
Imports System.Runtime.InteropServices
Imports System.Windows.Forms
Imports System.Globalization

''' <summary>
''' bla bla bla 
''' </summary>
<ComVisible(False)> _
Public Class xdmnCalender1

    ' Holds the picture we are drawing.
    Private m_Picture As New DrawablePicture(Me.BackColor)

    ' The current drawing attributes.
    Private m_CurrentLineWidth As Integer = 1
    Private m_CurrentForeColor As Color = Color.Black
    Private m_CurrentFillColor As Color = Color.White

    ' The object we are currently drawing.
    Private m_NewDrawable As Drawable

    ' The tool we have currently selected.
    Private m_SelectedToolButton As ToolBarButton

    ' The index of the first thickness image in its ImageList.
    Private m_FirstLineThicknessImage As Integer
    Private m_FirstLineColorImage As Integer
    Private m_FirstFillColorImage As Integer

    Private Sub InitializeComponent()
        Me.picCanvas = New System.Windows.Forms.PictureBox
        Me.ofdImage = New System.Windows.Forms.OpenFileDialog
        CType(Me.picCanvas, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'picCanvas
        '
        Me.picCanvas.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.picCanvas.Dock = System.Windows.Forms.DockStyle.Fill
        Me.picCanvas.Location = New System.Drawing.Point(0, 0)
        Me.picCanvas.Name = "picCanvas"
        Me.picCanvas.Size = New System.Drawing.Size(304, 169)
        Me.picCanvas.TabIndex = 0
        Me.picCanvas.TabStop = False
        '
        'ofdImage
        '
        Me.ofdImage.FileName = "ofdImage"
        '
        'dmnCalender1
        '
        Me.Controls.Add(Me.picCanvas)
        Me.Name = "dmnCalender1"
        Me.Size = New System.Drawing.Size(304, 169)
        CType(Me.picCanvas, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

    Private Sub picCanvas_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picCanvas.MouseDown, picCanvas.MouseDown
        ' Start drawing a rectangle.
        m_NewDrawable = New DrawableRectangle(m_CurrentForeColor, m_CurrentFillColor, m_CurrentLineWidth, e.X, e.Y, e.X, e.Y)
        m_Picture.Add(m_NewDrawable)
        AddHandler picCanvas.MouseMove, AddressOf NewDrawable_MouseMove
        AddHandler picCanvas.MouseUp, AddressOf NewDrawable_MouseUp
    End Sub
    'On mouse move, continue drawing.
    Private Sub NewDrawable_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        ' Update the new line's coordinates.
        m_NewDrawable.NewPoint(e.X, e.Y)

        ' Redraw to show the new line.
        picCanvas.Invalidate()
    End Sub

    ' On mouse up, finish drawing the new object.
    Private Sub NewDrawable_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        ' No longer watch for MouseMove or MouseUp.
        RemoveHandler picCanvas.MouseMove, AddressOf NewDrawable_MouseMove
        RemoveHandler picCanvas.MouseUp, AddressOf NewDrawable_MouseUp

        ' See if the new object is empty (e.g. a zero-length line).
        If m_NewDrawable.IsEmpty() Then
            ' Discard this object.
            m_Picture.Remove(m_NewDrawable)
        Else
            ' If it's a new image, get the image file.
            If TypeOf (m_NewDrawable) Is DrawableImage Then
                If ofdImage.ShowDialog() = Windows.Forms.DialogResult.OK Then
                    ' Discard this object.
                    Dim drawable_image As DrawableImage = DirectCast(m_NewDrawable, DrawableImage)
                    drawable_image.Picture = New Bitmap(ofdImage.FileName)
                Else
                    ' Discard this object.
                    m_Picture.Remove(m_NewDrawable)
                End If
            End If
        End If

        ' We're no longer working with the new object.
        m_NewDrawable = Nothing

        ' Redraw.
        picCanvas.Invalidate()
    End Sub

    '#End Region ' New object event handlers

End Class
